extern crate lazy_static;
extern crate serde_derive;
extern crate toml;

use lazy_static::lazy_static;
use serde_derive::Deserialize;
use std::fs::File;
use std::io::prelude::*;

lazy_static! {
    static ref CONFIG: Config = {
        let config_toml_path = "Forum.toml";
        let config_content = get_config_toml_content(config_toml_path);
        let config = toml::from_str::<Config>(&config_content).unwrap();
        config
    };
}

#[derive(Deserialize, Debug)]
pub struct Config {
    pub path: Path,
    pub web: Web,
}

#[derive(Deserialize, Debug)]
pub struct Web {
    pub address: String,
}

#[derive(Deserialize, Debug)]
pub struct Path {
    pub static_path: String,
    pub favicon_path: String,
}

pub fn get_global_config() -> &'static Config {
    &CONFIG
}

fn get_config_toml_content(config_toml_path: &str) -> String {
    let mut file = File::open(config_toml_path).unwrap();
    let mut content = String::new();
    file.read_to_string(&mut content).unwrap();
    content
}
